package com.lagou.hbasework;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;

public class CreateTable {
    Configuration conf = null;
    Connection connection = null;
    HBaseAdmin admin = null;

    @Before
    public void init() {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.20.100,192.168.20.101,192.168.20.102");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.master", "192.168.20.100:9001");
        try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void destroy() {
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void create_table() throws IOException {
        admin = (HBaseAdmin) connection.getAdmin();
        //创建表容器
        HTableDescriptor friendManner = new HTableDescriptor(TableName.valueOf("friendManner"));
        //设置列族描述器
        friendManner.addFamily(new HColumnDescriptor("friends"));
        //创建表
        admin.createTable(friendManner);
        System.out.println("friendManner表创建成功！！！");
    }

    @Test
    public void into_data() throws IOException {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf("friendManner"));
        //设定rowKey
        Put put = new Put(Bytes.toBytes("uid10"));
        //添加value
        Put column1 = put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        Put column2 = put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        Put column3 = put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid6"), Bytes.toBytes("uid6"));
        Put column4 = put.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid10"), Bytes.toBytes("uid10"));
        ArrayList<Put> puts = new ArrayList<>();
        puts.add(column1);
        puts.add(column2);
        puts.add(column3);
        puts.add(column4);
        table.put(puts);
        table.close();
        System.out.println("插入数据成功！！！");
    }

    @Test
    public void delete_operate() throws IOException {
        //获取表对象
        Table table = connection.getTable(TableName.valueOf("friendManner"));
        //获取删除对象
        Delete delete_master = new Delete(Bytes.toBytes("uid1"));
        //删除数据
        ArrayList<Delete> deletes = new ArrayList<>();
        deletes.add(delete_master);
        table.delete(deletes);
        //释放资源
        table.close();
        System.out.println("删除数据成功！！！！");
    }
}
